/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package CtrLayer;
import ModelLayer.*;
import DBLayer.*;
import java.util.ArrayList;

/**
 *
 * @author Sharif
 */
public class CtrSale {
    

    public CtrSale() {

    }

    public ArrayList findAllSalesOrders() {
	IFDBSalesOrder dbOrder = new DBSalesOrder();
	ArrayList allOrders = new ArrayList();
	allOrders = dbOrder.getAllSalesOrders(false);
	return allOrders;
    }

    public SalesOrder findById(int id) {
	IFDBSalesOrder dbOrder = new DBSalesOrder();
	return dbOrder.findSalesOrder(id, true);
    }

    public int updateSalesOrder(int id, String date, int deliveryStatus, String deliveryDate, int customerId, int invoiceId, double amount) {
	IFDBSalesOrder dbOrder = new DBSalesOrder();
	SalesOrder order = new SalesOrder();
	order.setId(id);
	order.setDate(date);
	order.setDeliveryStatus(deliveryStatus);
	order.setDeliveryDate(deliveryDate);
	order.setCustomer(customerId);
	order.setInvoice(invoiceId);
	order.setAmount(amount);
	return dbOrder.updateSalesOrder(order);
        }

        public void insertNew(String date, int deliveryStatus, String deliveryDate, int customerId, int invoiceId) {
	IFDBSalesOrder dbOrder = new DBSalesOrder();
	SalesOrder order = new SalesOrder();
	//order.setId(id);
	order.setDate(date);
	order.setDeliveryStatus(deliveryStatus);
	order.setDeliveryDate(deliveryDate);
	order.setCustomer(customerId);
	order.setInvoice(invoiceId);
	dbOrder.insertSalesOrder(order);
        }

       public void makeSalesOrderProduct(int salesOrderId, String productId, double amount)
        {
           Product product = new Product();
           SalesOrder salesOrder = new SalesOrder();
	   CtrProduct ctrP = new CtrProduct();
           product = ctrP.findById(productId);
           //salesOrder = CtrSO.findById(salesOrderId);
	   salesOrder = findById(salesOrderId);
           IFDBSalesOrderProduct dbSalesOrderProduct = new DBSalesOrderProduct();
           SalesOrderProduct salesOrderProduct = new SalesOrderProduct();
           
           salesOrderProduct.setSalesOrder(salesOrder);
           salesOrderProduct.setProduct(product);
           salesOrderProduct.setAmount(amount);
           dbSalesOrderProduct.insertNew(salesOrderProduct);
        }
}
